import { createRouter, createWebHashHistory } from 'vue-router'
import index from '../views/home/index.vue'
import cart from '../views/cart/cart.vue'
import order from '../views/order/order.vue'
import mine from "../views/mine/mine.vue"
import login from '../views/login.vue'
import register from '../views/register.vue'
import address from '../views/address.vue'
import newbuilt from '../views/newbuilt.vue'
import edit from '../views/edit.vue'
import merchant from '../views/merchant/merchant.vue'
import affirm from '../views/affirm/affirm.vue'
import goodsdetail from '../views/goodsdetail.vue'


const routes = [

    {
        path: '/',
        name: 'index',
        component: index
    },
    {
        path: '/cart',
        name: 'cart',
        component: cart
    },
    {
        path: '/goodsdetail/:id',
        name: 'goodsdetail',
        component: goodsdetail
    },
    {
        path: '/order',
        name: 'order',
        component: order
    },
    {
        path: '/mine',
        name: 'mine',
        component: mine
    },
    {
        path: '/address',
        name: 'address',
        component: address
    },
    {
        path: '/edit',
        name: 'edit',
        component: edit
    },
    {
        path: '/newbuilt',
        name: 'newbuilt',
        component: newbuilt
    },
    {
        path: '/register',
        name: 'register',
        component: register
    },
    {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: '/merchant',
        name: 'merchant',
        component: merchant
    },
    {
        path: '/affirm',
        name: 'affirm',
        component: affirm
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === "/cart" || to.path === "/order") {
        if (localStorage.getItem("isLogin") === "true") {
            next()
            console.log("成功");

        } else {
            router.push("/login")
            console.log("失败");
        }
    } else { next() }

})

export default router
